<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<jsp:include page="../inc.jsp"></jsp:include>
<script type="text/javascript" charset="utf-8">
	var datagrid;
	var auth = "${auths}";
	var tyid = "${sessionInfo.type}";
	var buttons = [];
	$(function() {
	    $('form input').bind('keyup', function(event) {/* 增加回车提交功能 */
			if (event.keyCode == '13') {
				datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
			}
		});
		
		if(auth=="all"){
			buttons.push({text : '增加',iconCls : 'icon-add',handler : function() {append();}});
			buttons.push('-');
			buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
			buttons.push('-');
			buttons.push({text : '编辑',iconCls : 'icon-edit',handler : function() {edit();}});
			buttons.push('-');
			buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
			buttons.push('-');
		}else if(auth!=""){
			var auths = auth.split(",");
			if(auths.length>0){
				for(var i=0;i<auths.length;i++){
					if(auths[i]=="SPGL_PPGL_ADD"){
						buttons.push({text : '增加',iconCls : 'icon-add',handler : function() {append();}});
						buttons.push('-');
					}else if(auths[i]=="SPGL_PPGL_DELETE"){
						buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
						buttons.push('-');
					}else if(auths[i]=="SPGL_PPGL_EDIT"){
						buttons.push({text : '编辑',iconCls : 'icon-edit',handler : function() {edit();}});
						buttons.push('-');
					}
				}
				buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
				buttons.push('-');
			}
		}

		datagrid = $('#datagrid').datagrid({
			url : 'brandAction!datagrid.html',
			title : '品牌列表',
			iconCls : 'icon-save',	
			pagination : true,
			pagePosition : 'bottom',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40 ],
			fit : true,
			fitColumns : false,
			nowrap : false,
			border : false,
			idField : 'id',
			sortName : 'name',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			singleSelect : true,
			frozenColumns : [ [ {
				title : '编号',
				field : 'id',
				width : 150,
				sortable : true,
				checkbox : true
			}, {
				title : '品牌名称',
				field : 'name',
				width : 150,
				sortable : true
			},{
				title : '折扣数量',
				field : 'dname',
				width : 150,
				hidden : false
			 },{
				title : '折扣ID',
				field : 'did',
				width : 150,
				hidden : true
			 }, {
				title : '所属仓库ID',
				field : 'rid1',
				width : 150,
				hidden : true
			}] ],
			columns : [ [ {
				title : '所属仓库',
				field : 'rname1',
				width : 350
			}, {
				title : '仓库品牌中间表ID',
				field : 'brids',
				width : 350,
				hidden : true
			}]],
			toolbar : buttons,
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
		
		if(tyid==5||tyid==2){
			$('#datagrid').datagrid('showColumn','dname')
		}else{
			$('#datagrid').datagrid('hideColumn','dname')
		}
		var rid0 = $('#searchForm').find('input[name=rid]');
		var rid0Combobox = rid0.combobox({
			url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
			valueField : 'id',
			textField : 'name',
			multiple : false,
			editable : false,
		});

	});

	function edit() {
		var rows = datagrid.datagrid('getSelections');
		if (rows.length == 1) {
			var p = parent.sy.dialog({
				title : '编辑品牌',
				href : '${pageContext.request.contextPath}/spgl/brandAction!brandEdit.html',
				width : 440,
				height : 135,
				buttons : [ {
					text : '编辑',
					handler : function() {
					var f = p.find('form');
					f.form('submit', {
						url : '${pageContext.request.contextPath}/spgl/brandAction!edit.html',
						success : function(d) {
							var json = $.parseJSON(d);
							if (json.success) {
								datagrid.datagrid('reload');
								p.dialog('close');
							}
							parent.sy.messagerShow({
								msg : json.msg,
								title : '提示'
							});
						}
					});
					
				}
				} ],
				onLoad : function() {
					var f = p.find('form');
					var did = f.find('input[name=did]');
					var didCombobox = did.combobox({
						url : '${pageContext.request.contextPath}/spgl/discountAction!noAuth_Combobox.html',
						valueField : 'id',
						textField : 'discountNum',
						multiple : false,
						editable : false
					});
					var rid = f.find('input[name=rid1]');
					var ridCombobox = rid.combobox({
						lines : true,
						url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
						valueField : 'id',
						textField : 'name',
						multiple : true,
						editable : false,
						onLoadSuccess : function() {
							parent.$.messager.progress('close');
						}
					});
					f.form('load', {
						id : rows[0].id,
						name : rows[0].name,
						rid1 : sy.getList(rows[0].rid1),
						remark:rows[0].remark,
						did : rows[0].did,
						brids : rows[0].brids,
					});
				}
			});
		} else if (rows.length > 1) {
			parent.sy.messagerAlert('提示', '同一时间只能编辑一条记录！', 'error');
		} else {
			parent.sy.messagerAlert('提示', '请选择要编辑的记录！', 'error');
		}
	}
	function append() {
		var p = parent.sy.dialog({
			title : '添加品牌',
			href : '${pageContext.request.contextPath}/spgl/brandAction!brandAdd.html',
			width : 440,
			height : 135,
			buttons : [ {
				text : '添加',
				handler : function() {
					var f = p.find('form');
					f.form('submit', {
						url : '${pageContext.request.contextPath}/spgl/brandAction!add.html',
						success : function(d) {
							var json = $.parseJSON(d);
							if (json.success) {
								datagrid.datagrid('reload');
								p.dialog('close');
							}
							parent.sy.messagerShow({
								msg : json.msg,
								title : '提示'
							});
						}
					});
				}
			} ],
			onLoad : function() {
				var f = p.find('form');
				var did = f.find('input[name=did]');
				var didCombobox = did.combobox({
					url : '${pageContext.request.contextPath}/spgl/discountAction!noAuth_Combobox.html',
					valueField : 'id',
					textField : 'discountNum',
					multiple : false,
					editable : false
				});
				var rid = f.find('input[name=rid1]');
				var ridCombobox = rid.combobox({
					url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
					valueField : 'id',
					textField : 'name',
					multiple : true,
					editable : false
				});
			}
		});
	}
	function remove() {
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.sy.messagerConfirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/spgl/brandAction!delete.html',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(d) {
							datagrid.datagrid('load');
							datagrid.datagrid('unselectAll');
							parent.sy.messagerShow({
								title : '提示',
								msg : d.msg
							});
						}
					});
				}
			});
		} else {
			parent.sy.messagerAlert('提示', '请勾选要删除的记录！', 'error');
		}
	}
	function _search() {
		datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
	}
	
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
	}
</script>
</head>
<body class="easyui-layout" data-options="fit:true">
	<div data-options="region:'north',border:false,title:'搜索条件'" style="height: 55px;overflow: hidden;" align="left">
		<form id="searchForm">
			<table class="tableForm datagrid-toolbar" style="width: 100%;height: 100%;">
				<tr>
					<th style="width:80px;">品牌名称</th>
					<td style="width:220px;"><input name="name" style="width:200px;" /></td>
					<th style="width:80px;">所属仓库</th>
					<td style="width:220px;"><input name="rname" style="width: 200px;" /></td>
					<td><a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">搜索</a><a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a></td>
				</tr>
			</table>
		</form>
	</div>
	<div data-options="region:'center',border:false" style="overflow: hidden;">
		<table id="datagrid"></table>
	</div>

	<div id="menu" class="easyui-menu" style="width:120px;display: none;">
		<k:security key="SPGL_PPGL_ADD">
			<div onclick="append();" data-options="iconCls:'icon-add'">增加</div>
		</k:security>
		<k:security key="SPGL_PPGL_DELETE">
			<div onclick="remove();" data-options="iconCls:'icon-remove'">删除</div>
		</k:security>
		<k:security key="SPGL_PPGL_EDIT">
			<div onclick="edit();" data-options="iconCls:'icon-edit'">编辑</div>
		</k:security>
	</div>
</body>
</html>